package com.jiuqian.demo.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jiuqian.common.core.base.BaseServiceImpl;
import com.jiuqian.demo.mapper.AreaMapper;
import com.jiuqian.demo.model.Area;
import com.jiuqian.demo.service.AreaService;

import java.util.List;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class AreaServiceImpl extends BaseServiceImpl<AreaMapper, Area> implements AreaService {
	private static final short PROVINCE = 1;
	private static final short CITY = 2;
	private static final short REGION = 3;
	private static final short STREET = 4;

	@Override
	@Cacheable(value = { "area-list" }, key = "#parentId")
	public List<Area> finByParentId(Integer parentId) {
		Wrapper<Area> wrapper = new EntityWrapper<Area>();
		wrapper.eq("parentid", parentId);
		return selectList(wrapper);
	}

	@Override
	public List<Area> findByLevel(Short level) {
		Wrapper<Area> wrapper = new EntityWrapper<Area>();
		wrapper.eq("level", level);
		return selectList(wrapper);
	}

	@Override
	public List<Area> findByParentIdAndLevel(Integer parentId, Short level) {
		Wrapper<Area> wrapper = new EntityWrapper<Area>();
		wrapper.eq("parentid", parentId).eq("level", level);
		return selectList(wrapper);
	}

	@Override
	@Cacheable({ "area-list" })
	public List<Area> findProvinces() {
		return findByLevel(PROVINCE);
	}

	@Override
	@Cacheable(value = { "area-list" }, key = "#provinceId")
	public List<Area> findCityByProvinceId(Integer provinceId) {
		return findByParentIdAndLevel(provinceId, CITY);
	}

	@Override
	@Cacheable(value = { "area-list" }, key = "#cityId")
	public List<Area> findRegionByCityId(Integer cityId) {
		return findByParentIdAndLevel(cityId, REGION);
	}

	@Override
	@Cacheable(value = { "area-list" }, key = "#regionId")
	public List<Area> findStreetByRegionId(Integer regionId) {
		return findByParentIdAndLevel(regionId, STREET);
	}

}
